package com.csproj.dispatch.resource;

import com.codahale.metrics.annotation.Timed;
import com.csproj.dispatch.service.PositionInfoService;
import com.csproj.dispatch.service.dto.PositionInfoDTO;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.system.domain.UserInfo;
import com.csproj.system.service.SysLogsService;
import com.csproj.system.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.prefs.BackingStoreException;

/**
 * REST controller for managing PositionInfo.
 * 位置信息controller
 */
@RestController
@RequestMapping("/api/center/posi")
public class PositionInfoResource {

    private final Logger log = LoggerFactory.getLogger(PositionInfoResource.class);

    private static final String ENTITY_NAME = "positionInfo";

    private final PositionInfoService positionInfoService;
    private final UserInfoService userInfoService;
    private final SysLogsService sysLogsService;
    public PositionInfoResource(PositionInfoService positionInfoService,UserInfoService userInfoService,SysLogsService sysLogsService) {
        this.positionInfoService = positionInfoService;
        this.sysLogsService = sysLogsService;
        this.userInfoService = userInfoService;
    }

    /**
     * 上传位置信息
     * @param positionInfoDTO 位置信息
     * @param lockCode 锁编号-集装箱编号
     * @return
     */
    @PostMapping("/positionInfo/{lockCode}")
    @Timed
    public BaseDTO insertPositionInfo(@RequestBody PositionInfoDTO positionInfoDTO, @PathVariable String lockCode){
        return positionInfoService.insertPositionInfo(positionInfoDTO, lockCode);
    }

    /**
     * 查看实时位置-最新一次位置记录
     * @param dispatchId 调度信息id
     * @return
     */
    @GetMapping("/positionInfo/nowposi/{dispatchId}")
    @Timed
    public BaseDTO searchPositionInfoNow(@PathVariable String dispatchId){

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_view_positionInfo","");
        return positionInfoService.searchPositionInfoNow(dispatchId);
    }

    /**
     * 查看历史轨迹-此次调度所有的位置信息
     * @param dispatchId 调度信息id
     * @return
     */
    @GetMapping("/positionInfo/oldposi/{dispatchId}")
    @Timed
    public BaseDTO searchPositionInfoOld(@PathVariable String dispatchId){
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_view_history_positionInfo","");
        return positionInfoService.searchPositionInfoOld(dispatchId);
    }

}